package com.coc.service;

import com.coc.bean.CcEloUser;
import com.coc.bean.CcMap;
import com.coc.dao.EloUserDAO;
import com.coc.util.MemCacheUtil;
import javax.servlet.http.HttpServletRequest;

public class CocBattleService extends BaseService {
	//战斗匹配
	//1） 玩家已经被判定离线
	//2） 玩家的防护罩已经过期
	//3） 奖杯数量级匹配
	//4） 玩家最近一次被匹配的“预计战斗结束时刻” < 当前时刻 - 开战限时 - PVP限时 - 冗余【暂定10分钟】
	// type = 1,2   1=匹配;  2=重新匹配  ;3=取消
	public void GetEloMatch(HttpServletRequest request, StringBuffer sbf) {
		try {
			long t = Long.parseLong(getParamValue("t", null, request));
			int id = Integer.parseInt(request.getParameter("tk").substring(0,
					request.getParameter("tk").indexOf("_")));
			CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
			CcEloUser ceu = getEloUserDAO(request).findByID(id);
			if ((t > System.currentTimeMillis()) || (t < ceu.getLastaction())) {
				sbf.append("{\"sign\":{\"value\":2}}");
				return;
			}
			ceu.setLastaction(t);
			getEloUserDAO(request).saveOrUpdate(ceu);
			int type = Integer.parseInt(request.getParameter("type"));
			long now = System.currentTimeMillis();
			//多久没操作视为离线,后期xml中配置
			int offline = 300000;
			//战斗准备时间和战斗时间,冗余时间
			int battleready = 30000;
			int battleneed = 180000;
			int battlered = 6000000;
			//cup区间
			int cup1 = 300;
			int cup2 = 500;
			//离线时刻
			long lastaction = now - offline;
			//最后匹配时刻
			long lastelo = now - battleneed-battleready-battlered;
			//把上一个匹配时间去掉(如果有上一个)
			CcEloUser cu_last = getEloUserDAO(request).findByID(ceu.getLasteloid());
			if(cu_last!=null){
				cu_last.setLastelotime(0);
				 getEloUserDAO(request).saveOrUpdate(cu_last);
			}
			if(type<3){
				CcEloUser cu_match = getEloUserDAO(request).findMatch(now,
						lastaction, lastelo, ceu.getCup() - cup1,
						ceu.getCup() + cup2);
				//匹配到了,把对面的匹配时间填上
				if (cu_match != null) {
					cu_match.setLastelotime(now);
					getEloUserDAO(request).saveOrUpdate(cu_match);
					ceu.setLasteloid(cu_match.getId());
					getEloUserDAO(request).saveOrUpdate(ceu);
					sbf.append("\"match\":{\"value\":" + cu_match.getId() + "}}");
				} else {
					sbf.append("\"match\":{\"value\":-1}}");
				}
			}else{
				
				sbf.append("{\"sign\":{\"value\":1}}");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			sbf.append("\"error\":{\"value\":0}}");
		}
	}
	//重新匹配
	public void GetEloReMatch(HttpServletRequest request, StringBuffer sbf) {
		try {
			long t = Long.parseLong(getParamValue("t", null, request));
			int id = Integer.parseInt(request.getParameter("tk").substring(0,
					request.getParameter("tk").indexOf("_")));
			CcEloUser ceu = getEloUserDAO(request).findByID(id);

			if ((t > System.currentTimeMillis()) || (t < ceu.getLastaction())) {
				sbf.append("{\"sign\":{\"value\":2}}");
				return;
			}
			ceu.setLastaction(t);
			getEloUserDAO(request).saveOrUpdate(ceu);
			long now = System.currentTimeMillis();

			int offline = 300000;
			int battleneed = 300000;
			int cup1 = 300;
			int cup2 = 500;
			long lastaction = now - offline;
			long lastelo = now - battleneed;
			CcEloUser cu_match = getEloUserDAO(request).findMatch(now,
					lastaction, lastelo, ceu.getCup() - cup1,
					ceu.getCup() + cup2);
			if (cu_match != null) {
				cu_match.setLastelotime(now);
				getEloUserDAO(request).saveOrUpdate(cu_match);
				ceu.setLasteloid(cu_match.getId());
				getEloUserDAO(request).saveOrUpdate(ceu);
				if (ceu.getLasteloid() != 0) {
					CcEloUser tmp = getEloUserDAO(request).findByID(
							ceu.getLasteloid());
					tmp.setLastelotime(now);
					getEloUserDAO(request).saveOrUpdate(tmp);
				}
				sbf.append("\"match\":{\"value\":" + cu_match.getId() + "}}");
			} else {
				sbf.append("\"match\":{\"value\":-1}}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			sbf.append("\"error\":{\"value\":0}}");
		}
	}

	public void GetEloCancel(HttpServletRequest request, StringBuffer sbf) {
		try {
			long t = Long.parseLong(getParamValue("t", null, request));
			int id = Integer.parseInt(request.getParameter("tk").substring(0,
					request.getParameter("tk").indexOf("_")));
			CcEloUser ceu = getEloUserDAO(request).findByID(id);

			if ((t > System.currentTimeMillis()) || (t < ceu.getLastaction())) {
				sbf.append("{\"sign\":{\"value\":2}}");
				return;
			}
			ceu.setLastaction(t);
			getEloUserDAO(request).saveOrUpdate(ceu);
			long now = System.currentTimeMillis();
			if (ceu.getLasteloid() != 0) {
				CcEloUser tmp = getEloUserDAO(request).findByID(
						ceu.getLasteloid());
				tmp.setLastelotime(now);
				getEloUserDAO(request).saveOrUpdate(tmp);
			}
			sbf.append("{\"sign\":{\"value\":1}}");
		} catch (Exception e) {
			e.printStackTrace();
			sbf.append("\"error\":{\"value\":0}}");
		}
	}
}

/*
 * Location: C:\Work\WorkSpace\classes\ Qualified Name:
 * com.coc.service.CocBattleService JD-Core Version: 0.6.2
 */